<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllerview //extends AppController
{

	function __construct(  )
	{
		
		$task	=	application::getTask();//( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			
			case 'getList':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getList(
										application::getUser_id(), 
										$parameters->id,
										$parameters->column,
										$parameters->order,
										$parameters->limit,
										$parameters->offset);
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
			    //catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    //}
				
		
				
				
				break;
				
			case 'getItem':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters 			=	application::getParameters();
					
					if( !empty($parameters->id)){
						
						$result 	=	$this->getItem(
										application::getUser_id(),
										$parameters->id);
					
						$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
									
					}else{
						
						$result=array("status"=>0,
									"data"=>MSG_36_FALTA_PARAMETRO,
									"notification"=>36);
						
					}			
					
											
		
					//if( count($result)>0 ){
					
						//var_dump( $result );
						//exit();
					//}else{
						//$result=array("view"=>"user exist");
					//}

					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			    //catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    //}
				
		
				
				
				break;
				
			
			case 'getColumnsTable':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters 			=	application::getParameters();
								
					$result 	=	$this->getColumnsTable(
													application::getUser_id(),
													$parameters->key);
					if($result){
						
						$result=array("status"=>1,
										"data"=>application::arrayToJson($result),
										"notification"=>-1);
									
					}else{
						
						$result=array("status"=>0,
										"data"=>MSG_29_NAO_FOI_POSSIVEL_RECUPERAR_COLUNAS,
										"notification"=>29);
					}
					
					

	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			    //catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    //}
				
		
				
				
				break;	
			case 'clone':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->viewClone(application::getUser_id(),
													$parameters);
					
					if( $result ){
						
						$result=array("status"=>1,
									"data"=>MSG_21_VIEW_CLONE_SUCESSO,
									"notification"=>21);
					}else{
						
						$result=array("status"=>0,
									"data"=>MSG_22_VIEW_CLONE_FALHA,
									"notification"=>22);
					}						
		
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			    //catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    //}
				
		
				
				
				break;			
							
			
					
			case 'save':
				
				
				try {
        						
								
					$parameters		=	application::getParameters();
								
					$id				=	$parameters->id;
					
					if(empty($id)){
						//sem id, então não é para criar um novo
		
						if( $this->save_view($parameters) ){
		
							$result=array("status"=>1,
									"data"=>MSG_14_VIEW_CRIADA_SUCESSO,
									"notification"=>14);
									
						}else{
							//erro ao salvar
							$result=array("status"=>0,
									"data"=>MSG_16_VIEW_FALHA_AO_CRIAR,
									"notification"=>16);
									
						}
					
			
					}else{
						
						$this->save_view($parameters);
						
						//$result=array("status"=>"e-mail Unknown");
						//$result=array("status"=>"update");
						$result=array("status"=>1,
									"data"=>MSG_15_VIEW_ATUALIZADA_SUCESSO,
									"notification"=>15);
					}
					
					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			  //  catch (Exception $e) {
			   //     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			   // }
				
		
				
				
				break;
			
			case 'delete':
				
				
				try {
        						
								
					$parameters		=	application::getParameters();
								
					$id				=	$parameters->id;
					
					if(!empty($id)){
						//sem id, então não é para criar um novo
		
						if( $this->delete_view(application::getUser_id(),
												$parameters) ){
							
							//$result=array("status"=>"delete sucess");
							$result=array("status"=>1,
									"data"=>MSG_18_VIEW_DELETE_SUCESSO,
									"notification"=>18);
									
						}else{
							//erro ao salvar
							//$result=array("status"=>"delete failed");
							$result=array("status"=>0,
									"data"=>MSG_19_ERRO_AO_EXCLUIR_VIEW,
									"notification"=>19);
						}
					
			
					}else{

						//$result=array("status"=>"failed not exist");
						$result=array("status"=>0,
									"data"=>MSG_20_VIEW_NAO_EXISTE,
									"notification"=>20);
					}
					
					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			  //  catch (Exception $e) {
			  //      echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			   // }
				
		
				
				
				break;
				
				
			case 'download':
				
				
				try {
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(false);
					
								
					$parameters 			=	application::getParameters();
								
					$result 	=	$this->getViewData(
										application::getUser_id(),$parameters);
					
					
					
					switch( $parameters->notation ){
						case 'csv':
							
							$result = application::arrayToCsv($result);
							break;
						case 'json':
							
							$result = application::arrayToJson($result);
							break;
					}
					
	
					application::executeView( 'notation', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			   // catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			   // }
				
		
				
				
				break;
			case 'out':
				
				
				try {
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(false);
					
								
					$parameters =	application::getParameters();
								
					if( $parameters->metadata ){
						
						$result 	=	$this->getViewMetaDataFromKey(
														$parameters->key,
														$parameters->notation);
														
					}else{
						
						$result 	=	$this->getViewDataFromKey(
														$parameters->key,
														$parameters->notation);
														
					}			
								
			
					application::executeView( 'notation', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			  //  catch (Exception $e) {
			   ////     echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			  //  }
				
		
				
				
				break;	
			default:
				
				//echo "view não definida.";
				throw new AppException( MSG_23_METODO_NAO_DEFINIDO );
				
				break;
		}
		
	}
	

	
	public function getViewMetaDataFromKey( $table_name, $notation ){
				
		$result=false;
				
		try{
			
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getViewMetaDataFromKey( $table_name, 
																$notation );
			
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
				
				
		return $result;
		
	}
	
	
	
	
	public function getViewDataFromKey( $table_name, $notation ){
				
		$result=false;
			
		try{
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getViewDataFromKey( $table_name, $notation );
																
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
						
						
	public function setViewDataFromKeyPeriodoUpdate( $table_name ){
				
		$result=false;
			
		try{
					
			$model	=	application::getModel('view');
			
			$result	=	$model->setViewDataFromKeyPeriodoUpdate( $table_name );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
						
	public function getViewDataFromKeyPeriodo( $table_name ){
				
		$result=false;
			
		try{
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getViewDataFromKeyPeriodo( $table_name );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		

		return $result;
		
	}
	
	
	
	public function getViewData( $user_id, $parameters ){
				
		$result=false;
		
		try{
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getViewData( $user_id, $parameters );
			
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
	

	
	
	
	public function getList( $user_id, $id, $column, $order="asc", $limit=10, $offset=0 ){
				
		$result=false;

		try{
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getList( $user_id, 
											$id, 
											$column, 
											$order, 
											$limit, 
											$offset );

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;

	}
	


	public function getColumnsTable( $user_id, $table_name ){
				
		$result=false;
		
		try{
			
			$model	=	application::getModel('view');
			
			$result	=	$model->getColumnsTable( $user_id, $table_name );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }

		return $result;
		
	}
	
	


	
	
	public function getItem( $user_id, $id ){
				
		$result=false;
		
		try{

			$model	=	application::getModel('view');
	
			$result	=	$model->getItem( $user_id, $id );

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		
		return $result;
		
	}
	
	
	public function viewClone( $user_id, $parameters ){
				
		$result=false;
		
		try {
				
			$model	=	application::getModel('view');
	
			$result	=	$model->viewClone( $user_id, $parameters );
			
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }

		return $result;
		
	}
	
	
	
	
	public function table_is_user( $table_name, $user_id ){
				
		$result=false;
				
		try {
        	
			$model	=	application::getModel('view');
	
			$result	=	$model->table_is_user(  $table_name, $user_id );					
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	
	public function exists_table( $table_name ){
				
		$result=false;
				
		try {
        			
			$model	=	application::getModel('view');
	
			$result	=	$model->exists_table( $table_name );	
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	
	public function delete_transformation( $user_id, $id ){
				
		$result=false;
				
		try {
        		
				
			$model	=	application::getModel('transformation');
	
			$result	=	$model->delete_transformation( $user_id, $id );
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	public function delete_view( $user_id, $parameters ){
				
		$result=false;
				
		try {
        		
				
			$model	=	application::getModel('view');
	
			$result	=	$model->delete_view( $user_id, $parameters );
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	public function save_transformation( $parameters ){
				
		$result=false;
				
		try {
			
			$model	=	application::getModel('view');
	
			$result	=	$model->save_transformation( $parameters );
		
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
		
	public function save_view( $parameters ){
				
		$result=false;
				
		try {
			
			$model	=	application::getModel('view');
	
			$result	=	$model->save_view( $parameters );
		
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	
}